@echo off
setlocal enabledelayedexpansion

:: Get current script directory
set "CURRENT_DIR=%~dp0"
:: Remove trailing backslash
set "CURRENT_DIR=%CURRENT_DIR:~0,-1%"

echo ================================
echo PATH Environment Variable Add Tool
echo ================================
echo Current directory: %CURRENT_DIR%
echo.

:: Check administrator privileges
echo Checking administrator privileges...
net session >nul 2>&1
if %errorLevel% == 0 (
    echo [OK] Administrator privileges detected, will add to system environment variables
    set "TARGET=SYSTEM"
) else (
    echo [INFO] No administrator privileges, will add to user environment variables
    set "TARGET=USER"
)

echo.

:: Get current PATH environment variable
echo Reading current PATH environment variable...
if "%TARGET%"=="SYSTEM" (
    echo Reading PATH from system registry...
    for /f "tokens=2*" %%a in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PATH 2^>nul') do set "CURRENT_PATH=%%b"
) else (
    echo Reading PATH from user registry...
    for /f "tokens=2*" %%a in ('reg query "HKCU\Environment" /v PATH 2^>nul') do set "CURRENT_PATH=%%b"
)

if not defined CURRENT_PATH (
    echo [WARNING] No existing PATH variable found, will create new PATH
) else (
    echo [OK] Successfully read PATH variable
)

:: If PATH doesn't exist, create an empty one
if not defined CURRENT_PATH set "CURRENT_PATH="

:: Check if path already exists
echo Checking if path already exists...
echo %CURRENT_PATH% | findstr /i /c:"%CURRENT_DIR%" >nul
if %errorLevel% == 0 (
    echo [INFO] Path "%CURRENT_DIR%" already exists in PATH environment variable
    echo No need to add again.
    echo.
    goto :end
) else (
    echo [OK] Path does not exist, can be added
)

:: Add path to PATH
echo Building new PATH variable...
if defined CURRENT_PATH (
    set "NEW_PATH=%CURRENT_PATH%;%CURRENT_DIR%"
    echo [OK] Will append new path to existing PATH
) else (
    set "NEW_PATH=%CURRENT_DIR%"
    echo [OK] Creating new PATH variable
)

:: Update registry
echo Updating registry...
if "%TARGET%"=="SYSTEM" (
    echo Attempting to write to system environment variables...
    reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PATH /t REG_EXPAND_SZ /d "!NEW_PATH!" /f
    if !errorLevel! == 0 (
        echo [SUCCESS] Successfully added "%CURRENT_DIR%" to system PATH environment variable
    ) else (
        echo [ERROR] Failed to add to system PATH ^(Error code: !errorLevel!^)
        echo Please ensure you run this script as administrator
        goto :error
    )
) else (
    echo Attempting to write to user environment variables...
    reg add "HKCU\Environment" /v PATH /t REG_EXPAND_SZ /d "!NEW_PATH!" /f
    if !errorLevel! == 0 (
        echo [SUCCESS] Successfully added "%CURRENT_DIR%" to user PATH environment variable
    ) else (
        echo [ERROR] Failed to add to user PATH ^(Error code: !errorLevel!^)
        goto :error
    )
)

echo.
echo ================================
echo Operation Complete!
echo ================================
echo [INFO] Environment variable changes may require:
echo     - Restart command prompt/PowerShell
echo     - Re-login to Windows user
echo     - Or run 'refreshenv' command
echo.

:: Broadcast environment variable change message
echo Notifying system of environment variable changes...
powershell -Command "& {[System.Environment]::SetEnvironmentVariable('PATH', [System.Environment]::GetEnvironmentVariable('PATH', 'Machine'), 'Process')}" 2>nul
if !errorLevel! == 0 (
    echo [OK] Environment variable change notification sent
) else (
    echo [WARNING] Failed to send environment variable change notification, but PATH was successfully added
)

echo.
echo [SUCCESS] Path addition complete!
echo You can now use programs from: %CURRENT_DIR%
echo.
echo To verify, open a new command prompt and type:
echo   echo %%PATH%%
echo.
goto :end

:error
echo.
echo ================================
echo Operation Failed
echo ================================
echo Please check the following possible causes:
echo 1. Insufficient privileges ^(try running as administrator^)
echo 2. Registry access blocked
echo 3. System security policy restrictions
echo.
goto :end

:end
echo.
echo Press any key to exit...
pause >nul
exit /b 0